<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
class Activity extends Controller{
    //活动页
    public function activitylist()
    {
        $nets=Db::name("activity")->select();
        // dump($nets);
        // exit;
		$this->assign("nets",$nets);
        return $this->fetch();
    }
    //添加活动页
    public function addactivity()
    {
        return $this->fetch();
    }
    public function addactivity1()
    {
        $data=input('post.');
        $files = request()->file('activity_pic');
        // dump($files);
        // exit;
        $filename=array();
        foreach ($files as $file) 
        {
            // 移动到框架应用根目录/public/uploads/ 目录下
            
            $look = $file->rule('uniqid')->validate(['size' => 5242880, 'ext' => 'jpg,jpeg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static' . DS .'uploads');
            $filename[]=$look->getFilename();
            if ($look) {
                // 成功上传后 获取上传信息
                // dump($info);
                //echo $look->getFilename();
                
            } else {
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
        // dump($filename);
        // exit;
        $data=[
            'activity_title'=>input('activity_title'),
            'activity_matter'=>input('activity_matter'),
            'activity_site'=>input('activity_site'),
            'activity_begin_time'=>input('activity_begin_time'),
            'activity_end_time'=>input('activity_end_time'),
            'activity_time'=>date('Y-m-d H:i:s',time()),
            'activity_status'=>0,
            'activity_pic'=>implode("###", $filename),
        ];
        // dump($data);
        // exit;
        if(Db::name('activity')->insert($data))
        {
        return $this->success('添加成功','activity/activitylist');
        }else
        {
            $this->error('失败，请重新填写信息');
        }
    }
    //修改活动页
    
    public function updateactivity($hid)
    {
        $nets=Db::name("activity")->field("hid,htitle,hdetails,hsite,hphone,hpic")->where('hid',$hid)->select();
        // dump($nets);
        // exit;
		$this->assign("nets",$nets);
        return $this->fetch();
    }
    //处理修改活动页
    public function updateactivity1($hid)
    {
        $data=input('post.');
        $files = request()->file('hpic');
        // dump($data);
        // exit;
        $filename=array();
        foreach ($files as $file) 
        {
            // 移动到框架应用根目录/public/uploads/ 目录下
            
            $look = $file->rule('uniqid')->validate(['size' => 5242880, 'ext' => 'jpg,jpeg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static' . DS .'uploads');
            $filename[]=$look->getFilename();
            if ($look) {
                // 成功上传后 获取上传信息
                // dump($info);
                //echo $look->getFilename();
                
            } else {
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
        // dump($filename);
        // exit;
        if($filename==null){
            $data=[
                'htitle'=>input('htitle'),
                'hdetails'=>input('hdetails'),
                'hsite'=>input('hsite'),
                'hphone'=>input('hphone'),
                'hdes'=>0,
                // 'hpic'=>implode("###", $filename),
            ];
            // dump($data);
            // exit;
            if(Db::name('activity')->where('hid',$hid)->update($data))
            {
            return $this->success('修改成功','activity/activitylist');
            }else
            {
                $this->error('失败，请重新填写信息');
            }
        }
        else{
            $data=[
                'htitle'=>input('htitle'),
                'hdetails'=>input('hdetails'),
                'hsite'=>input('hsite'),
                'hphone'=>input('hphone'),
                'hdes'=>0,
                'hpic'=>implode("###", $filename),
            ];
            // dump($data);
            // exit;
            if(Db::name('activity')->where('hid',$hid)->update($data))
            {
            return $this->success('修改成功','activity/activitylist');
            }else
            {
                $this->error('失败，请重新填写信息');
            }
        }
        
    }
    //删除比赛
    public function deleteactivity($hid)
    {
        $data=[
            
            'hdes'=>1,
           
        ];
        // dump($data);
        // exit;
        if(Db::name('activity')->where('hid',$hid)->update($data))
        {
        return $this->success('删除成功','activity/activitylist');
        }else
        {
            $this->error('删除失败，请重新操作','activity/activitylist');
        } 
    }
    //活动报名列表
    public function useractivity($hid)
    {
        $nets=Db::name('bactivity b')->join("user u","b.uid=u.uid")->where('hid',$hid)->select();
        // dump($nets);
        // exit;
		$this->assign("nets",$nets);
        return $this->fetch();
        // $hid=controller('hid');
    }
    // 你好，我做一个测试
}